<?php
namespace Rac\Controller;
use Think\Controller;
use Think\Auth;
class BaseController extends Controller {
	
	
	//protected $lang = 'zh-cn';
	protected $admin_id = 1;
	
	public function _initialize() {

    	$admin = session('admin');
    	if (empty($admin)) {
    		$this->redirect('Login/index');
    	}
    	//$member = D('Member')->getAccountById($member['id']);
        $this->assign('admin', $admin); //这里取最新的数据 
		
		
		
		//session存在时，不需要验证的权限
		$not_check = array(
				'Rac/Group/add_form', 
				'Rac/News/news_edit_form', 
				'Rac/News/news_add_form',
				'Rac/Group/rules_form', 
			    'Rac/Admin/add_form', 
				'Rac/Matcher/add_form',
				'Rac/Matcher/matcher_edit_form',  
				'Rac/Sys/add',
				'Rac/Sys/add_form',
				'Rac/Sys/sysvar_del',
				'Rac/Sys/sysvar_edit_form', 
				'Rac/Member/update2' ,
				'Rac/Withdraw/withdraw_adopt_select',
				'Rac/News/addbanner',
				'Rac/News/addbanner_form',
				'Rac/News/banner_del',
				'Rac/Member/add',
				'Rac/Presentation/getNodeChild',
				'Rac/Withdraw/not_withdraw_button',
				'Rac/Withdraw/thaw_withdraw_button'
	
		);
		
		//当前操作的请求                 模块名/方法名
		if(! in_array(MODULE_NAME.'/'.CONTROLLER_NAME.'/'.ACTION_NAME, $not_check)){
			//下面代码动态判断权限
			$auth = new Auth();
			//echo MODULE_NAME.'/'.CONTROLLER_NAME.'/'.ACTION_NAME;
			if(!$auth->check(MODULE_NAME.'/'.CONTROLLER_NAME.'/'.ACTION_NAME, $this->admin_id) && session('aid') != 1){
				if (IS_AJAX) {
					echo json_encode(array('flag'=>-99, 'msg'=>'您没有权限'));
					die;
				}
				$this->error('没有权限');
				
				//echo '  not role';die;
			}
		}
		
		
		
		// 这里到时候用一个缓存 目前先不用做
		$cache_key = 'sys_nav_' . LANG_SET;
		F($cache_key,  null);
		$nav = F($cache_key);
		if (empty($nav)) {
			$nav = $this->get_nav();
	        F($cache_key, $nav);
		}
		
	
		$this->assign('nav', $nav);
    }
	
    
    // 获取有权限的菜单
    public function get_nav() {
    	$auth = new Auth();
    	$auth_role = $auth->getAuthList($this->admin_id, 1);

    	return D('Nav')->auth_nav($auth_role);
    }

    public function index(){
    	/* setcookie("user", "runoob", time()+3600);
    	
    	$this->display(); */
    }
}